﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using System.Data;
using Microsoft.Reporting.WebForms;
using System.Text;

namespace Catering.Catering_Page.Kufang
{
    public partial class XiaohaoTypePortait : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            int[] RAWTID = UCSearchToolWithDate1.GetRaw();
            string AREAID = DDLTool1.GetDDL_Area();
            string CLASSID = DDLTool1.GetDDL_Class();
            string year = DDLTool1.GetYear();
            if (string.IsNullOrEmpty(year)) return;
            if (RAWTID == null || RAWTID.Count() <= 0) return;
            DateTime DTIME = DateTime.Parse(year + "-01");
            string CompanyID = "0";
            if (Session["UserID"] != null)
            {
                int userid = Convert.ToInt32(Session["UserID"]);
                CompanyID = new Catering_Rules.T_Personnel().GetCompanyIDByPID(userid).ToString();
            }
            View_Monitor vm = new View_Monitor();
            DataTable dt = vm.GetXiaohaoTypePortaitThird(RAWTID, CLASSID, AREAID, CompanyID, DTIME);
            Dictionary<string, Dictionary<string, decimal>> zhonglei = new Dictionary<string, Dictionary<string, decimal>>();
            foreach (string rawName in UCSearchToolWithDate1.GetRawName())
            {
                Dictionary<string, decimal> list = GetReportModel();//班组列表(x轴列表)
                if (!zhonglei.ContainsKey(rawName)) zhonglei.Add(rawName, list);
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    decimal sumnumber = Convert.ToDecimal(dt.Rows[i]["SumNumber"]);
                    int temprawtid = Convert.ToInt32(dt.Rows[i]["RAWTID"]);
                    string typename = dt.Rows[i]["TypeName"].ToString();
                    int tempmonth = Convert.ToInt32(dt.Rows[i]["YMonth"]);
                    if (zhonglei.ContainsKey(typename))
                    {
                        zhonglei[typename][tempmonth + "月"] = decimal.Round(sumnumber);
                    }
                }
                dt.Dispose();
            }
            string str = "";
            StringBuilder tableStr = new StringBuilder("");
            if (zhonglei != null)
            {
                int n = 0;
                StringBuilder Sresult = new StringBuilder("");
                tableStr.Append("<table cellpadding='0' cellspacing='0' align='center' width='98%' border='1'><tr>");
                tableStr.Append("<th width='16%'>&nbsp;</th><th width='7%'>1月</th><th width='7%'>2月</th><th width='7%'>3月</th><th width='7%'>4月</th>");
                tableStr.Append("<th width='7%'>5月</th><th width='7%'>6月</th><th width='7%'>7月</th><th width='7%'>8月</th><th width='7%'>9月</th><th width='7%'>10月</th><th width='7%'>11月</th><th width='7%'>12月</th></tr>");
                foreach (string raw in zhonglei.Keys)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + raw + "',");
                    Sresult.Append("data: [");
                    tableStr.Append("<tr><th>" + raw + "</th>");
                    string StrData = "";
                    foreach (string key in zhonglei[raw].Keys)
                    {
                        StrData += zhonglei[raw][key] + ",";
                        if (zhonglei[raw][key] == 0) tableStr.Append("<td>&nbsp;</td>"); else tableStr.Append("<td>" + zhonglei[raw][key] + "</td>");
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    Sresult.Append("},");
                    tableStr.Append("</tr>");
                    str = Sresult.ToString();
                    n++;
                }
                tableStr.Append("</table>");
            }
            string title = Bind_Data_to_Controls.Bind_ReportTitle(AREAID, CLASSID, DateTime.Parse(DTIME.Year+"-1-1 12:11:11"), "消耗分类纵向对比");
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + title + "'," + str + "); SetTableData(\"" + tableStr.ToString() + "\");", true);
            
        }

        public Dictionary<string, decimal> GetReportModel()
        {
            Dictionary<string, decimal> result = new Dictionary<string, decimal>();
            for (int i = 1; i <= 12; i++)
            {
                if (!result.ContainsKey(i + "月")) result.Add(i + "月", 0);
            }
            return result;
        }
    }
}